home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / api / query.js < prev    next >
Text File  |  2008-03-26  |  8KB  |  321 lines

  1. /*
  2.     api/query.js
  3.  
  4.     Copyright ┬⌐ 2005, 2006, 2007  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. var wot_api_query =
  8. {
  9.     init: function()
  10.     {
  11.         this.message = "";
  12.         this.message_id = "";
  13.         this.message_type = "";
  14.         this.message_url = "";
  15.         this.users = new Array();
  16.     },
  17.  
  18.     send: function(hostname, callback)
  19.     {
  20.         try {
  21.             if (!wot_api_register.ready) {
  22.                 return false;
  23.             }
  24.  
  25.             if (wot_cache.iscached(hostname) &&
  26.                     (wot_cache.get(hostname, "pending") ||
  27.                         wot_cache.get(hostname, "inprogress"))) {
  28.                 return false;
  29.             }
  30.  
  31.             wot_cache.create(hostname);
  32.             wot_cache.set(hostname, "time", Date.now());
  33.             wot_cache.set(hostname, "inprogress", true);
  34.             wot_cache.set(hostname, "status", WOT_QUERY_ERROR);
  35.  
  36.             var nonce = wot_crypto.nonce();
  37.             var context =
  38.                 wot_arc4.create(wot_hash.hmac_sha1hex(wot_prefs.witness_key,
  39.                     nonce));
  40.  
  41.             if (!context) {
  42.                 wot_cache.set(hostname, "inprogress", false);
  43.                 return false;
  44.             }
  45.  
  46.             var crypted_target =
  47.                 wot_arc4.crypt(context,
  48.                     wot_hash.strtobin(wot_idn.utftoidn(hostname)));
  49.  
  50.             if (!crypted_target) {
  51.                 wot_cache.set(hostname, "inprogress", false);
  52.                 return false;
  53.             }
  54.  
  55.             var query_string = WOT_SERVICE_API_QUERY +
  56.                 "?id="         + wot_prefs.witness_id +
  57.                 "&nonce="    + nonce +
  58.                 "&target="    + encodeURIComponent(btoa(
  59.                                 wot_hash.bintostr(crypted_target))) +
  60.                 "&lang="    + wot_util.getstring("language") +
  61.                 "&version=" + WOT_PLATFORM + "-" + WOT_VERSION;
  62.  
  63.             var request = new XMLHttpRequest();
  64.  
  65.             wot_cache.add_nonce(nonce, hostname);
  66.  
  67.             request.open("GET", WOT_SERVICE_NORMAL +
  68.                 wot_crypto.authenticate_query(query_string));
  69.  
  70.             new wot_cookie_remover(request);
  71.             request.timeout = null;
  72.  
  73.             request.onload = function(event)
  74.             {
  75.                 try {
  76.                     if (request.timeout) {
  77.                         window.clearTimeout(request.timeout);
  78.                         request.timeout = null;
  79.                     }
  80.  
  81.                     wot_cache.set(hostname, "time", Date.now());
  82.  
  83.                     if (request.status == 200) {
  84.                         wot_cache.add_query(
  85.                             request.responseXML.getElementsByTagName(
  86.                                 WOT_SERVICE_XML_QUERY),
  87.                             request.responseXML.getElementsByTagName(
  88.                                 WOT_SERVICE_XML_QUERY_TARGET),
  89.                             false);
  90.  
  91.                         wot_api_query.init();
  92.  
  93.                         wot_api_query.parse_messages(
  94.                             request.responseXML.getElementsByTagName(
  95.                                     WOT_SERVICE_XML_QUERY_MSG));
  96.  
  97.                         wot_api_query.parse_users(
  98.                             request.responseXML.getElementsByTagName(
  99.                                     WOT_SERVICE_XML_QUERY_USER));
  100.  
  101.                         wot_api_query.parse_status(
  102.                             request.responseXML.getElementsByTagName(
  103.                                     WOT_SERVICE_XML_QUERY_STATUS));
  104.                     }
  105.  
  106.                     wot_cache.set(hostname, "inprogress", false);
  107.                     wot_cache.remove_nonce(nonce);
  108.                     wot_core.update();
  109.  
  110.                     if (typeof(callback) == "function") {
  111.                         callback();
  112.                     }
  113.                 } catch (e) {
  114.                     dump("wot_api_query.onload: failed with " + e + "\n");
  115.                 }
  116.             };
  117.  
  118.             request.send(null);
  119.  
  120.             /* If we don't receive data reasonably soon, retry */
  121.             request.timeout = window.setTimeout(this.timeout,
  122.                 WOT_TIMEOUT_QUERY, request, hostname, callback);
  123.  
  124.             return true;
  125.         } catch (e) {
  126.             dump("wot_api_query.send: failed with " + e + "\n");
  127.         }
  128.         return false;
  129.     },
  130.  
  131.     timeout: function(request, hostname, callback) /* XMLHttpRequest */
  132.     {
  133.         try {
  134.             if (request.timeout) {
  135.                 window.clearTimeout(request.timeout);
  136.                 request.timeout = null;
  137.             }
  138.  
  139.             if (!wot_cache.get(hostname, "inprogress")) {
  140.                 return;
  141.             }
  142.  
  143.             dump("wot_api_query.timeout: for " + hostname + "\n");
  144.  
  145.             request.abort();
  146.             wot_cache.set(hostname, "time", Date.now());
  147.             wot_cache.set(hostname, "inprogress", false);
  148.             wot_core.update();
  149.             
  150.             if (typeof(callback) == "function") {
  151.                 callback();
  152.             }
  153.         } catch (e) {
  154.             dump("wot_api_query.timeout: failed with " + e + "\n");
  155.         }
  156.     },
  157.  
  158.     parse_messages: function(messages)
  159.     {
  160.         try {
  161.             if (!messages) {
  162.                 return;
  163.             }
  164.  
  165.             var i = 0;
  166.             var m = messages.item(0);
  167.             var type, target, version, than, url;
  168.  
  169.             while (m) {
  170.                 /* Display the first message that is targeted to us */
  171.                 msgid    = m.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_MSG_ID);
  172.                 type    = m.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_MSG_TYPE);
  173.                 url     = m.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_MSG_URL);
  174.                 target  = m.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_MSG_TARGET);
  175.                 version = m.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_MSG_VERSION);
  176.                 than    = m.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_MSG_THAN);
  177.  
  178.                 /* Must have mandatory fields */
  179.                 if (msgid && msgid.nodeValue && type && type.nodeValue &&
  180.                     target && target.nodeValue &&
  181.                     m.firstChild && m.firstChild.nodeValue &&
  182.                         (target.nodeValue == WOT_SERVICE_XML_QUERY_MSG_TARGET_ALL ||
  183.                             target.nodeValue == WOT_PLATFORM)) {
  184.                     /* A message targeted to our platform */
  185.                     if (version && version.nodeValue && than && than.nodeValue) {
  186.                         /* A versioned message */
  187.                         if ((version.nodeValue ==
  188.                                     WOT_SERVICE_XML_QUERY_MSG_VERSION_EQ &&
  189.                                 Number(WOT_VERSION) == Number(than.nodeValue)) ||
  190.                             (version.nodeValue ==
  191.                                     WOT_SERVICE_XML_QUERY_MSG_VERSION_LE &&
  192.                                 Number(WOT_VERSION) <= Number(than.nodeValue)) ||
  193.                             (version.nodeValue ==
  194.                                     WOT_SERVICE_XML_QUERY_MSG_VERSION_GE &&
  195.                                 Number(WOT_VERSION) >= Number(than.nodeValue))) {
  196.                             /* Targeted to us */
  197.                             this.message_id = msgid.nodeValue;
  198.                             this.message_type = type.nodeValue;
  199.                             this.message = m.firstChild.nodeValue;
  200.                             if (url && url.nodeValue) {
  201.                                 this.message_url = url.nodeValue;
  202.                             }
  203.                             break;
  204.                         }
  205.                     } else {
  206.                         /* Targeted to us */
  207.                         this.message_id = msgid.nodeValue;
  208.                         this.message_type = type.nodeValue;
  209.                         this.message = m.firstChild.nodeValue;
  210.                         if (url && url.nodeValue) {
  211.                             this.message_url = url.nodeValue;
  212.                         }
  213.                         break;
  214.                     }
  215.                 }
  216.  
  217.                 m = messages.item(++i);
  218.             }
  219.         } catch (e) {
  220.             dump("wot_api_query.parse_messages: failed with " + e + "\n");
  221.         }
  222.     },
  223.  
  224.     parse_users: function(users)
  225.     {
  226.         try {
  227.             this.users = new Array();
  228.  
  229.             if (!users) {
  230.                 return;
  231.             }
  232.  
  233.             var i = 0;
  234.             var u = users.item(0);
  235.             var a;
  236.  
  237.             while (u) {
  238.                 var item = new Object();
  239.                 a = u.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_USER_ICON);
  240.  
  241.                 if (a && a.nodeValue) {
  242.                     item.icon = a.nodeValue;
  243.                 }
  244.  
  245.                 a = u.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_USER_BAR);
  246.  
  247.                 if (a && a.nodeValue) {
  248.                     item.bar = a.nodeValue;
  249.                 }
  250.  
  251.                 a = u.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_USER_LENGTH);
  252.  
  253.                 if (a && a.nodeValue) {
  254.                     item.length = a.nodeValue;
  255.                 }
  256.  
  257.                 a = u.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_USER_LABEL);
  258.  
  259.                 if (a && a.nodeValue) {
  260.                     item.label = a.nodeValue;
  261.                 }
  262.  
  263.                 a = u.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_USER_URL);
  264.  
  265.                 if (a && a.nodeValue) {
  266.                     item.url = a.nodeValue;
  267.                 }
  268.  
  269.                 a = u.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_USER_TEXT);
  270.  
  271.                 if (a && a.nodeValue) {
  272.                     item.text = a.nodeValue;
  273.                 }
  274.  
  275.                 a = u.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_USER_NOTICE);
  276.  
  277.                 if (a && a.nodeValue) {
  278.                     item.notice = a.nodeValue;
  279.                 }
  280.  
  281.                 if (item.text && (!item.bar ||
  282.                         (item.length != null && item.label))) {
  283.                     this.users[i] = item;
  284.                 }
  285.  
  286.                 u = users.item(++i);
  287.             }
  288.         } catch (e) {
  289.             dump("wot_api_query.parse_users: failed with " + e + "\n");
  290.         }
  291.     },
  292.  
  293.     parse_status: function(stats)
  294.     {
  295.         try {
  296.             wot_prefs.clear("status_level");
  297.  
  298.             if (!stats) {
  299.                 return;
  300.             }
  301.  
  302.             var s = stats.item(0);
  303.  
  304.             if (!s) {
  305.                 return;
  306.             }
  307.  
  308.             var l = s.attributes.getNamedItem(WOT_SERVICE_XML_QUERY_STATUS_LEVEL);
  309.  
  310.             if (l && l.nodeValue) {
  311.                 wot_prefs.setChar("status_level", l.nodeValue);
  312.             }
  313.  
  314.         } catch (e) {
  315.             dump("wot_api_query.parse_status: failed with " + e + "\n");
  316.         }
  317.     }
  318. };
  319.  
  320. wot_api_query.init();
  321.